class EventBus {
  constructor() {
    this.EventBus = {}
  }

  on(eventName, callback, thisArg) {
    let handlers = this.EventBus[eventName]
    if (!handlers) {
      handlers = []
      this.EventBus[eventName] = handlers
    }
    handlers.push({
      callback,
      thisArg
    })
  }


  off(eventName, callback) {
    let handlers = this.EventBus[eventName]
    if (!handlers) return
    const temp = [...handlers]
    for (let i = 0; i < temp.length; i++) {
      if (temp[i].callback == callback) {
        let index = handlers.indexOf(temp[i])
        handlers.splice(index, 1)
      }
    }
  }

  emit(eventName, ...args) {
    let handlers = this.EventBus[eventName]
    if (!handlers) return
    handlers.forEach(handler => {
      handler.callback.apply(handler.thisArg, args)
    });
  }
}

let eventBus = new EventBus()

const handler = function (arg = 'xjh3') {
  console.log('监听abc', this, arg)
}

eventBus.on('abc', handler, { name: 'xjh' })

eventBus.emit('abc', 123)

eventBus.off('abc', handler)

eventBus.emit('abc', 4343)